// devops/pages/power/power.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    State: [
      {label: '配电设备', id: 1},
      {label: '配电设备采集器' , id: 2}
    ],
    currentNav: 1,
    page: 1,
    isShowBottom: false,
    hasNext:false,
    isRequest:true,
    showSearch: true,
    dataList: [],
    page2: 1,
    collectList: [],
    isRequest2: true,
    hasNext2: false,
    clickOne: 0,
    showDel: false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  pageChange(val){
    console.log('组件传过来的值', val)
   
    if(this.data.currentNav ==1){
      this.setData({
        page: val.detail,
      })
      this.getList()
    }else{
      this.setData({
        page2: val.detail,
      })
      this.getCollect()
    }
    
  },
  showDelete(e){
    this.setData({
      showDel: !this.data.showDel,
      delId: e.currentTarget.dataset.id,
      delType: e.currentTarget.dataset.type,
    })
  },
  bandDevice(e){
    console.log('e',e)
    let id = e.currentTarget.dataset.id
    let path = e.currentTarget.dataset.path
    wx.navigateTo({
      url: `/devops/pages/power/device?id=${id}&&path=${path}`,
    })

  },
  delete(e){
    let that = this
    let id = this.data.delId
    let url = this.data.delType == 1? `/devops/power/box/device/delete/${id}` : `/devops/power/box/collector/delete/${id}`
    service("GET",url).then(res => {
      console.log('删除', res)
      if(res.data.code ==0){
        if(that.data.delType ==1){
          that.getList()
        }else{
          that.getCollect()
        }
        that.setData({
          showDel: false,
          delType: '',
          delId: ''
        })
      }
      
    })
  },

  editCollect(e){
    console.log('e',e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/devops/pages/power/collect?id='+id,
    })

  },
  //列表
    //自定义上拉加载
  loadMore(type) {
    //如果总页数 小于等于 当前页数 提示无更多数据
    if(!this.data.isRefresh){
      console.log('加载中')
      if (this.data.hasNext) {
        this.setData({
          page: this.data.page + 1,
          isRefresh:true
        })
        this.selectComponent("#orderlist").noMore('loading')
        this.getList(2)
      } else{
        console.log('没有更多')
        this.selectComponent("#orderlist").noMore('nomore')
        console.log('没有更多')
      }
    }
  },
  refresh(e){
    console.log('父组件刷新',e)
    this.setData({
      page: 1,
    })
    this.getList(1)
  },
  getList(){
    let that = this
    //节流
    if(that.data.isRequest === false){
      return
    }
    this.setData({
      isRequest: false
    })
    let sear = {page:this.data.page}
    service("POST","/devops/power/box/device/list",sear).then(res => {
      console.log('info', res)
      that.setData({
        isRefresh: false,
        isRequest: true
      })
    
      if(res.data.code == 0){
        that.setData({
          dataList: res.data.page.list,
          isRefresh: false,
          hasNext:res.data.page.hasNext,
        })
      }
    })
  },
  getCollect(){
    let that = this
    //节流
    if(that.data.isRequest2 === false){
      return
    }
    this.setData({
      isRequest2: false
    })
    let sear = {page:this.data.page2}
    service("POST","/devops/power/box/collector/list",sear).then(res => {
      console.log('info', res)
      that.setData({
        isRefresh2: false,
        isRequest2: true
      })
     
      if(res.data.code == 0){
        that.setData({
          collectList: res.data.page.list,
          isRefresh2: false,
          hasNext2:res.data.page.hasNext,
        })
      }
    })
  },
    //查看报修订单
    goOrder(){
      wx.redirectTo({
        url: '/devops/pages/order/order',
      })
    },
    goRepair(){
      wx.redirectTo({
        url: '/devops/pages/repair/repair',
      })
    },
    goPower(){
      wx.redirectTo({
        url: '/devops/pages/power/power',
      })
    },
    goPile(){
      wx.redirectTo({
        url: '/devops/pages/pile/index',
      })
    },
    goCollect(){
      wx.navigateTo({
        url: '/devops/pages/power/collect',
      })
    },
    goDevice(){
      wx.navigateTo({
        url: '/devops/pages/power/device',
      })
    },
  changeTab(e) {
    this.setData({
     currentNav:e.target.dataset.id,
   })
   if(this.data.currentNav ==1){
    this.getList()
   }else{
     this.getCollect()
   }
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let order = this.selectComponent("#orderlist")
    this.setData({
      orderlist: order
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.currentNav==1){
      this.getList()
    }else{
      this.getCollect()
    }
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})